package com.jindun.paper.ui.setting;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

import com.jindun.paper.model.Setting;

public class SettingListViewFilter extends ViewerFilter {

	private String searchString;

	private String catalog;

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public void setSearchText(String s) {
		// ensure that the value can be used for matching
		this.searchString = ".*" + s + ".*";
	}

	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {

		Setting clientVo = (Setting) element;

		boolean valueMatched = true;

		if (this.searchString != null) {
			valueMatched = StringUtils.isNotBlank(clientVo.getValue())
					&& clientVo.getValue().matches(searchString);
		}

		boolean catalogMatched = true;
		if (catalog != null) {
			catalogMatched = StringUtils.isNotBlank(clientVo.getCatalog())
					&& clientVo.getCatalog().matches(catalog);
		}

		return valueMatched && catalogMatched;
	}

}
